﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pata.SylkSharp.Core.Utils
{
    /// <summary>
    /// Helper class to parse enumeration.
    /// </summary>
    public class EnumParser
    {
        /// <summary>
        /// Parse a string into an enumeration
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T Parse<T>(string value) where T : struct
        {
            return (T)Enum.Parse(typeof(T), value);
        }

        /// <summary>
        /// Parse a string into an enumeration
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T Parse<T>(string value, T defaultValue) where T : struct
        {
            T result;
            return (Enum.TryParse<T>(value, out result))
                ? result
                : defaultValue;
        }
    }
}
